<template>
  <div class="carry-serve-section">
    <div class="send-content">
      <div class="title"><span><em>子任务</em><b>{{updateServeItem.stName}}</b></span></div>
      <div class="send-form">
        <span>执行时间</span>
        <div class="date-picker" @click="selectDate">
          <span>{{date}}</span>
          <i class="iconfont icon-arrow"></i>
        </div>
        <span>执行事项</span>
        <textarea 
          class="textarea-item"
          v-model="message"
        ></textarea>
        <span>执行图片</span>
        <div class="uploader">
          <van-uploader 
            v-model="fileList"
            multiple 
            :max-count="1"
            :after-read="afterRead"
          />
        </div>
      </div>
    </div>
    <div class="sendbtn">
      <button @click="handleConfirm">确认执行</button>
    </div>
    <van-calendar
      color="#1989fa"
      v-model="dateShow"
      @confirm="onDateConfirm" 
    />
  </div>
</template>

<script>
import moment from 'moment'
import { mapState } from 'vuex'
import {
	uploadImg,
  subTaskUpdate
} from "../../../request/api"
export default {
  name: "carryServe",
  data(){
    return {
      date:"",
      dateShow: false,
      message:"",
      fileList: [],
      fileUrl:""
    }
  },

	computed:{
		...mapState({
			updateServeItem: state=>state.serve.updateServeItem,
		})
	},
  methods:{
    selectDate(){
      this.dateShow = true
    },
    onDateConfirm(date){
      console.log(date)
      this.date = moment(date).format("YYYY-MM-DD HH:mm:ss");
      this.dateShow = false
    },
    async afterRead(file){
      file.status = 'uploading';
      file.message = '上传中...';

      let formData = new FormData()
      formData.append("file",file.file)
      const res = await uploadImg(formData)

      if(res.success){
        file.status = 'success';
        this.fileUrl = res.data
        file.beforeDelete = ()=>{
          this.fileUrl = ""
          return true
        }
      }else{
        file.status = 'failed';
        file.message = '上传失败';
      }
    },
    async handleConfirm(){
      console.log(this.date,this.message,this.fileUrl)
      const stFinishTime = this.date
      const stFinishContent = this.message
      const stFinishPic = this.fileUrl
      if(!stFinishTime){
        this.$toast({
          message: '请选择日期',
					position: 'bottom',
        })
        return
      }
      if(!stFinishContent){
        this.$toast({
          message: '请输入执行事项',
					position: 'bottom',
        })
        return
      }
      if(!stFinishPic){
        this.$toast({
          message: '请上传图片',
					position: 'bottom',
        })
        return
      }

      const { stId } = this.updateServeItem
      const res = await subTaskUpdate({
        stId,
        stFinishTime,
        stFinishContent,
        stFinishPic,
        stState:2
      })
      if(res.success){
        this.$toast({
					message: '执行成功',
					position: 'bottom',
				})
        // 跳转
        this.$router.replace({
          path:"/serve",
          query:{
            from:"carryServe"
          }
        })
        this.$store.commit("serve/clearCurrentSubServeInfo")
        this.message = ""
        this.date = ""
        this.fileUrl = ""
        this.fileList = []
      }else{
        this.$toast({
					message: '执行失败，请重新操作',
					position: 'bottom',
				})
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../../assets/style/variable.scss";
.carry-serve-section{
  height: 100%;
	background: $bg-gray;
  box-sizing: border-box;
  padding: 50px;
  .send-content{
    height: calc(100% - 130px);
    box-sizing: border-box;
    padding: 20px 50px;
    border-radius: 10px;
    background: #fff;
    .title{
      height: 100px;
      @include flexCenter;
      justify-content: flex-start;
      border-bottom: 1px solid rgba(0,0,0,.1);
      span{
        @include flexCenter;
        em{
          font-size: 20px;
          background: $color-blue;									
          border-radius: 4px;
          padding: 1px 5px;
          margin-right: 5px;
          color: #fff;
        }
        b{
          font-size: 36px;
          font-weight: bold;
        }
      }
    }
    .send-form{
      span{
        display: inline-block;
        font-size: 24px;
        margin: 30px 0 20px;
      }
      .date-picker{
        width: 100%;
        height: 70px;
        background: #F3F3F3;
        border-radius: 10px;
        @include flexCenter;
        justify-content: space-between;
        padding: 0 20px;
        span{
          font-size: 22px;
        }
      }
      .textarea-item{
        font-size: 24px;
        padding: 20px;
        width: 100%;
        display: block;
        height: 200px;
        background: #F3F3F3;
        border-radius: 10px;
      }
    }
  }
  .sendbtn{
    margin-top: 40px;
    button{
      height: 90px;
      width: 100%;
      border-radius: 50px;
      background: #3E9DFD;
      color: #fff;
      font-size: 32px;
    }
  }
}
</style>